package com.lix.langrensha.rest;

import com.alibaba.fastjson.JSONObject;
import com.lix.langrensha.contants.CommonStatusCode;
import com.lix.langrensha.model.Room;
import com.lix.langrensha.model.User;
import com.lix.langrensha.service.LangRenShaService;
import com.lix.langrensha.utils.MyWebSocket;
import com.lix.langrensha.utils.RtnJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @Author lix
 * @create 2019/12/4-16:46
 * @description
 */
@RestController
@RequestMapping("/lrs")
@Api(tags = "langrensha接口")
public class LangRenShaController {

    @Autowired
    private LangRenShaService langRenShaService;
    @Autowired
    private MyWebSocket webSocket;


    @GetMapping("/sendMessage")
    @ApiOperation("发送消息")
    public void sendMessage(@RequestParam String msg) throws IOException {
       MyWebSocket.sendInfo("发给你的"+msg);
    }
    @GetMapping("/createRoom")
    @ApiOperation("创建房间")
    public Room createRoom(@RequestParam String homeownerId,@RequestParam String roleJson) {
        return langRenShaService.createRoom(homeownerId,roleJson);
    }

    @GetMapping("/roomStatus")
    @ApiOperation("房间状态")
    public RtnJson roomStatus(@RequestParam Integer roomId)  {
        Room room = langRenShaService.roomInfo(roomId);
        if (null == room) {
            return RtnJson.toFail(CommonStatusCode.E00010007);
        }
        return RtnJson.toSuccess(room);
    }

    @GetMapping("/login")
    @ApiOperation("登陆")
    public String getWxUserInfo(@RequestParam String code){
        //请求获取微信响应数据
        JSONObject respJson = null;
        try {
            respJson = langRenShaService.getWxUserInfo(code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        assert respJson != null;
        return respJson.toString();
    }


    @PostMapping("/seating")
    @ApiOperation("入座")
    public RtnJson seating(@RequestParam Integer roomId,
                           @RequestParam String username,
                           @RequestParam String userId,
                           @RequestParam String avatarUrl,
                           @RequestParam Integer seatNum
                        )  {
        return langRenShaService.Seating(roomId,username,userId,avatarUrl,seatNum);
    }

    @GetMapping("/userInfo")
    @ApiOperation("获取用户详情")
    public User userInfo(@RequestParam Integer roomId,@RequestParam String userId)  {
        return langRenShaService.userInfo(roomId,userId);
    }

    @GetMapping("/shuffle")
    @ApiOperation("重新发牌")
    public RtnJson shuffle(@RequestParam Integer roomId,@RequestParam String homeownerId)  {
        return langRenShaService.shuffle(roomId,homeownerId);
    }

    @GetMapping("/setDead")
    @ApiOperation("设置座位死亡/复活")
    public RtnJson setDead(@RequestParam Integer roomId,
                           @RequestParam String homeownerId,
                           @RequestParam Integer seatNum,
                           @RequestParam Integer hp)  {
        return langRenShaService.setDead(roomId,homeownerId,seatNum,hp);
    }
    @GetMapping("/startVote")
    @ApiOperation("设置 开始/结束 投票")
    public RtnJson startVote(@RequestParam Integer roomId,
                           @RequestParam String homeownerId,
                             @RequestParam boolean flag)  {
        return langRenShaService.startVote(roomId,homeownerId,flag);
    }
    @GetMapping("/vote")
    @ApiOperation("投票")
    public RtnJson vote(@RequestParam Integer roomId, @RequestParam Integer seatNum,@RequestParam Integer target)  {
        return langRenShaService.vote(roomId,seatNum,target);
    }
    @GetMapping("/deleteRoom")
    @ApiOperation("删除房间/删除全部用户")
    public void deleteRoom(@RequestParam Integer roomId)  {
         langRenShaService.deleteRoom(roomId);
    }

    @GetMapping("/night")
    @ApiOperation("进入黑夜")
    public RtnJson night(@RequestParam Integer roomId,
                         @RequestParam String homeownerId
//                         @RequestParam Integer roomStatus
                         )  {
        return langRenShaService.night(roomId,homeownerId);
    }

    @GetMapping("/setStatus")
    @ApiOperation("setStatus")
    public RtnJson setStatus(@RequestParam Integer roomId,@RequestParam Integer status
    )  {
        return langRenShaService.setStatus(roomId,status);
    }

    @GetMapping("/useSkill")
    @ApiOperation("使用技能")
    public RtnJson useSkill(@RequestParam Integer roomId,
                             @RequestParam Integer effect,
                             @RequestParam Integer seatNum,
                             @RequestParam Integer target,
                             @RequestParam Integer roleId)  {
        return langRenShaService.useSkill(roomId,effect,seatNum,target,roleId);
    }

       @GetMapping("/langRenFindSkill")
    @ApiOperation("狼人查看队友技能")
    public RtnJson langRenFindSkill(@RequestParam Integer roomId)  {
        return langRenShaService.langRenFindSkill(roomId);
    }

    @GetMapping("/nvWuViewDead")
    @ApiOperation("女巫查看死亡人员")
    public RtnJson nvWuViewDead(@RequestParam Integer roomId)  {
        return langRenShaService.nvWuViewDead(roomId);
    }


}
